package com.example.my_rabbitmq_high_demo.api.ack;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

/**
 * @Description: 生产者
 * @Author: GaoJian
 * @Date: 2022/7/9 12:48
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1、创建ConnectionFactory并设置属性
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setVirtualHost("/");

        //2、创建连接
        Connection connection = factory.newConnection();

        //3、创建channel
        Channel channel = connection.createChannel();

        String exchangeName = "test_ack_exchange";
        String routingKey = "ack.qiye";

        for (int i = 0; i < 5; i++) {
            //发送消息
            String msg = "hello,七夜丶雪" + i;
            HashMap<String, Object> header = new HashMap<>();
            header.put("num", i);
            AMQP.BasicProperties properties = new AMQP.BasicProperties()
                    .builder()
                    .contentEncoding("UTF-8")
                    .headers(header)
                    .build();
            channel.basicPublish(exchangeName, routingKey, true, properties, msg.getBytes());
        }
        //关闭连接
        channel.close();
        connection.close();
    }
}
